//
//  ProductCustom.m
//  cutter
//
//  Created by mac on 16/1/8.
//  Copyright © 2016年 e360. All rights reserved.
//
#import "UIMenuClass.h"
#import <CommonCrypto/CommonDigest.h>
#import "TMGlobal.h"
#import "SpaceViewController.h"
@interface ProductSearchPopViewControler ()
@end

@implementation ProductSearchPopViewControler
-(void)dealloc{
    self.searchPopFinishedBlock = nil;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.dataArray = @[@"楼盘",@"空间",@"产品",@"搭配"];
}
#pragma mark - UITabelViewDelegate
- (void)setExtraCellLineHidden: (UITableView *)tableView{
    UIView *view =[ [UIView alloc]init];
    view.backgroundColor = [UIColor clearColor];
    [tableView setTableFooterView:view];
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 54;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = (UITableViewCell *)[tableView dequeueReusableCellWithIdentifier:@"UITableViewCell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"UITableViewCell"];
    }
    cell.textLabel.text = self.dataArray[indexPath.row];
    cell.textLabel.font = [UIFont systemFontOfSize:14.0];
    cell.textLabel.textColor = [UIColor colorWithHex:0x4a4a4a];
    cell.textLabel.textAlignment = 1;
    if ([cell.textLabel.text isEqualToString:self.currentTitle]) {
        cell.backgroundColor = [UIColor colorWithHex:0xf6f9fe];
    }else{
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    }
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (self.searchPopFinishedBlock) {
        self.searchPopFinishedBlock((int)indexPath.row);
    }
}
-(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [tableView setSeparatorInset:UIEdgeInsetsZero];
    }
    if ([tableView respondsToSelector:@selector(setLayoutMargins:)]) {
        [tableView setLayoutMargins:UIEdgeInsetsZero];
    }
    if ([cell respondsToSelector:@selector(setLayoutMargins:)]) {
        [cell setLayoutMargins:UIEdgeInsetsZero];
    }
}
@end
//

@implementation UIMenuClass
-(instancetype)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updataClient:) name:@"globalValueChange" object:nil];
    }
    return self;
}
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
-(void)awakeFromNib{
    [super awakeFromNib];
    [self updataClient:nil];
}
-(void)updataClient:(NSNotification*)sender{
    if (sender == nil || [[sender object] isEqualToString:@"client"]) {
        if([TMGlobal valueForKey:@"client"]){
            id name = [[TMGlobal valueForKey:@"client"] objectForKey:@"truename"];
            if (isEmpty(name) == NO) {
                self.nameView.text = name;
            }
        }else{
            self.nameView.text = @"选择客户";
        }
    }
}
-(IBAction)searchTouch:(UIButton *)sender{
    [UIView beginAnimations:nil context:nil];
    sender.hidden = YES;
    self.searchView.hidden = NO;
    [UIView commitAnimations];
    
}
-(IBAction)clearTouch:(UIButton *)sender{
    self.textField.text = @"";
}
-(IBAction)cancleTouch:(id)sender {
    [UIView beginAnimations:nil context:nil];
    self.searchbtn.hidden = NO;
    self.searchView.hidden = YES;
    [UIView commitAnimations];
}
-(IBAction)roomChange:(UIButton*)sender{
    ProductSearchPopViewControler *controller = [[ProductSearchPopViewControler alloc] init];
    [controller setModalPresentationStyle:UIModalPresentationPopover];
    [controller setPreferredContentSize:CGSizeMake(110, 216)];
    [controller setCurrentTitle:self.currentTitle];
    //
    __weak ProductSearchPopViewControler *weak = controller;
    controller.searchPopFinishedBlock = ^(int index){
        [TMGlobal setValue:@(index) forKey:@"homeIndex"];
        [weak dismissViewControllerAnimated:YES completion:nil];
    };
    //
    UIPopoverPresentationController *popoverPresentation = controller.popoverPresentationController;
    [popoverPresentation setSourceRect:sender.bounds];
    [popoverPresentation setSourceView:sender];
    //
    [self.viewController presentViewController:controller animated:YES completion:nil];
}
-(UIViewController*)viewController{
    UIResponder *next = self;
    while (next) {
        if ([next isKindOfClass:UIViewController.class]) {
            return (id)next;
        }
        next = next.nextResponder;
    }
    return nil;
}
@end

